/**
 * Initialize your data structure here. Set the size of the deque to be k.
 * @param {number} k
 */
 var MyCircularDeque = function(k) {
  this.k = k
  this.cnt = 0
  this.front = k - 1
  this.rear = 0
  this.arr = []
};

/**
* Adds an item at the front of Deque. Return true if the operation is successful. 
* @param {number} value
* @return {boolean}
*/
MyCircularDeque.prototype.insertFront = function(value) {
  if (this.cnt === this.k) {
      return false
  }
  this.arr[this.front]=value
  this.front = (this.front-1+this.k)%this.k
  ++this.cnt
  return true
};

/**
* Adds an item at the rear of Deque. Return true if the operation is successful. 
* @param {number} value
* @return {boolean}
*/
MyCircularDeque.prototype.insertLast = function(value) {
  if (this.cnt === this.k) {
      return false
  }
  this.arr[this.rear]=value
  this.rear = (this.rear+1)%this.k
  ++this.cnt
  return true
};

/**
* Deletes an item from the front of Deque. Return true if the operation is successful.
* @return {boolean}
*/
MyCircularDeque.prototype.deleteFront = function() {
  if (this.cnt === 0) {
      return false
  }
  this.front = (this.front+1)%this.k
  --this.cnt
  return true
};

/**
* Deletes an item from the rear of Deque. Return true if the operation is successful.
* @return {boolean}
*/
MyCircularDeque.prototype.deleteLast = function() {
  if (this.cnt === 0) {
      return false
  }
  this.rear = (this.rear-1+this.k)%this.k
  --this.cnt
  return true
};

/**
* Get the front item from the deque.
* @return {number}
*/
MyCircularDeque.prototype.getFront = function() {
  if (this.cnt === 0) {
      return -1
  }
  return this.arr[(this.front+1)%this.k]
};

/**
* Get the last item from the deque.
* @return {number}
*/
MyCircularDeque.prototype.getRear = function() {
  if (this.cnt === 0) {
      return -1
  }
  return this.arr[(this.rear-1+this.k)%this.k]
};

/**
* Checks whether the circular deque is empty or not.
* @return {boolean}
*/
MyCircularDeque.prototype.isEmpty = function() {
return this.cnt === 0
};

/**
* Checks whether the circular deque is full or not.
* @return {boolean}
*/
MyCircularDeque.prototype.isFull = function() {
  return this.cnt === this.k
};

var circularDeque = new MyCircularDeque(3); // 设置容量大小为3
circularDeque.insertLast(1);			        // 返回 true
circularDeque.insertLast(2);			        // 返回 true
circularDeque.insertFront(3);			        // 返回 true
circularDeque.insertFront(4);			        // 已经满了，返回 false
circularDeque.getRear();  				// 返回 2
circularDeque.isFull();				        // 返回 true
circularDeque.deleteLast();			        // 返回 true
circularDeque.insertFront(4);			        // 返回 true
circularDeque.getFront();				// 返回 4
